package com.iotp.controller.admin.base;

import com.iotp.entity.base.CodeEnum;
import com.iotp.entity.user.CommUser;
import com.iotp.utils.common.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class BaseController {

    @Autowired
    protected HttpServletRequest request;// HttpServletRequest
    @Autowired
    protected HttpServletResponse response;// HttpServletResponse
    protected int basePageNo = 1;// 分页默认页码
    protected int basePageOffset = 0;// 分页默认记录索引
    protected int basePageSize = 5;// 分页默认一页记录数

    /**
     * 获取分页页码
     * @param pageNo 当前页码
     * @return Integer 页码
     */
    protected Integer getPageNo(Integer pageNo) {
        if(pageNo == null) {
            pageNo = basePageNo;
        }
        return pageNo;
    }

    /**
     * 获取分页记录索引
     * @param pageOffset 当前记录索引
     * @return Integer 页码
     */
    protected Integer getPageOffset(Integer pageOffset) {
        if(pageOffset == null) {
            pageOffset = basePageOffset;
        }
        return pageOffset;
    }

    /**
     * 获取分页一页记录数
     * @param pageSize 记录数
     * @return Integer 记录数
     */
    protected Integer getPageSize(Integer pageSize) {
        if(pageSize == null) {
            pageSize = basePageSize;
        }
        return pageSize;
    }

    /**
     * 根据页码和记录数获取首条记录索引
     * @param pageNo 页码
     * @param pageSize 记录数
     * @return Integer 首条记录索引
     */
    protected Integer pageNo2FirstResult(Integer pageNo, Integer pageSize) {
        return (pageNo - 1) * pageSize;
    }

    protected void setSession(String key, Object value){
        request.getSession().setAttribute(key, value);
    }

    protected CommUser getCurUser(){
        return (CommUser) request.getSession().getAttribute(CodeEnum.当前用户.msg);
    }

//    protected Organization getCurOrg(){
//        return (Organization) request.getSession().getAttribute(CodeEnum.当前用户机构.msg);
//    }

    /**
     * 获取当前登录用户Id
     */
    protected Long getUserId() {
//        Subject subject = SecurityUtils.getSubject();
//        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
//        return user.getUserId();
        CommUser user = (CommUser) request.getSession().getAttribute(CodeEnum.当前用户.msg);
//        CommUser user = (CommUser) request.getSession().getAttribute(CodeEnum.当前用户机构.msg);
        return user.getNewuserId();
    }

    protected String getOldUserId() {
//        Subject subject = SecurityUtils.getSubject();
//        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
//        return user.getUserId();
//        CommUser user = (CommUser) request.getSession().getAttribute(CodeEnum.当前用户机构.msg);
        CommUser user = (CommUser) request.getSession().getAttribute(CodeEnum.当前用户.msg);
        return user.getUserId();
    }

    public boolean getIsSuperManager() {
        if( request.getSession().getAttribute("isSuperManager") == null) {
            return false;
        }
        return (Boolean) request.getSession().getAttribute("isSuperManager");
    }

    // 设置&获取用户机构信息

    public Long getOrgId() {
        return (Long) request.getSession().getAttribute("orgId");
    }

    public void setOrgId(Long orgId) {
        request.getSession().setAttribute("orgId", orgId);
    }

    public String getOrgCode() {
        return (String) request.getSession().getAttribute("orgCode");
    }

    public void setOrgCode(String orgCode) {
        request.getSession().setAttribute("orgCode", orgCode);
    }

    public Integer getOrgType() {
        return (Integer) request.getSession().getAttribute("orgType");
    }

    public void setOrgType(Integer orgType) {
        request.getSession().setAttribute("orgType", orgType);
    }

    /**
     * 验证字符串属性是否合法
     * @param property 字符串属性
     * @param maxLength 最大长度
     * @param isAll true-验证全部（非空 + 长度） | false-只验证长度（可为空）
     * @return true-合法 | false-不合法
     */
    protected boolean validateStringProperty(String property, int maxLength, boolean isAll) {
        if(isAll) {
            if(!DataUtil.isEmpty(property) && DataUtil.validateLength(property, 1, maxLength)) {
                return true;
            } else {
                return false;
            }
        } else {
            if(!DataUtil.isEmpty(property)) {
                if(DataUtil.validateLength(property, 1, maxLength)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return true;
            }
        }
    }

}
